{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "![](https://raw.githubusercontent.com/Qinbf/tf-model-zoo/master/README_IMG/01.jpg)\n",
    "AI MOOC： **www.ai-xlab.com**  \n",
    "如果你也是AI爱好者，可以添加我的微信一起交流：**sdxxqbf**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.cluster import DBSCAN\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 载入数据\n",
    "data = np.genfromtxt(\"kmeans.txt\", delimiter=\" \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DBSCAN(algorithm='auto', eps=1.5, leaf_size=30, metric='euclidean',\n",
       "    min_samples=4, n_jobs=1, p=None)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 训练模型\n",
    "# eps距离阈值，min_samples核心对象在eps领域的样本数阈值\n",
    "model = DBSCAN(eps=1.5, min_samples=4)\n",
    "model.fit(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1, -1,  2,  0,\n",
       "        1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1,\n",
       "        2,  3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,\n",
       "        3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3,\n",
       "        0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3], dtype=int64)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = model.fit_predict(data)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD8CAYAAABjAo9vAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAFlFJREFUeJzt3V9sZGd5x/Hf42XXxAoskuMakY1n\nWFGtiLJIbSzEigsiFqoQEnLTC+gkipILqwikRCWiUEtFXFi9QIJcgEBWC6rYkWglQC0oFYTAqjdL\nhTckLGlYFFn2EhKMScWCtGjzx08vxpO1x8eec2bOn/e85/uRrGRnvTPveMa/ec77Puc95u4CAMRj\nouoBAADyRbADQGQIdgCIDMEOAJEh2AEgMgQ7AESGYAeAyBDsABAZgh0AIvO6Kh70hhtu8Ha7XcVD\nA0BtnT9//nfuPjPs+yoJ9na7rZWVlSoeGgBqy8zW03wfUzEAEBmCHQAiQ7ADQGQIdgCIDMEOAJEh\n2AH0dLtSuy1NTPT+2+1WPSKMiGAH0AvxhQVpfV1y7/13YSGucG/QBxfBDkBaXJSuXNl925Urvdtj\n0IQPrh0IdgDSpUvZbq+b2D+4BhDsAKS5uWy3103sH1wDCHYA0tKSNDW1+7apqd7tMYj9g2sAwQ5A\n6nSk5WWp1ZLMev9dXu7dHoPYP7gGVLIJGIAAdTrxBPmg/vNaXOxNv8zN9UI90udLsANohpg/uAYw\nFQMAkSHYASAyBDsARIZgB4DIEOwAEBmCHQAiQ7ADQGQIdgCIDMEOAJEh2AEgMgQ70CQNuopQkxHs\nKAwZEpiGXUWoyQh2FIIMCVDDriLUZAQ7CkGGBKhhVxFqMoIdhSBDAtSwqwg1GcGOQpAhAWrYVYSa\nLLdgN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InJUW/kOHu33f3V7b/+GNJx6ocTwHe\nKelZd19195ckfUPS3RWPqTDu/oK7P7H9/39UL/BurHZUxTGzY5I+KOmfqx5LHgj2gpjZhyT92t2f\nqnosFXhA0n9VPYic3SjpVzv+/JwiDrqdzKwt6S8k/U+1IynUI+oVYVtVDyQPXGhjDGb2A0lvTvir\nRUn/IOmvyh1RsQ56vu7+H9vfs6jeYXy3zLGVwBJui/5IzMyul/RNSQ+5+x+qHk8RzOxOSb919/Nm\ndlvV48kDwT4Gd39f0u1mdlLSWyU9ZWZSb1riCTN7p7v/psQh5mq/59tnZvdJulPSaY+vj/Y5STft\n+PMxSc9XNJZSmNlh9UK96+7fqno8BXq3pA+Z2R2SXi/pjWZ2xt3vqXhcI6OPvQRmtiZp3t1D3lxo\nLGZ2u6TPS3qPu29WPZ68mdnr1FsUPi3p15J+Iulv3P3pSgdWEOtVJP8q6f/c/aGqx1OW7Yr9YXe/\ns+qxjIM5duTli5LeIOkxM3vSzL5S9YDytL0w/HFJ31NvIfHfYw31be+WdK+k926/nk9uV7SoASp2\nAIgMFTsARIZgB4DIEOwAEBmCHQAiQ7ADQGQIdgCIDMEOAJEh2AEgMv8PQMbAElXOc+oAAAAASUVO\nRK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2a7d4db45c0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 画出各个数据点，用不同颜色表示分类\n",
    "mark = ['or', 'ob', 'og', 'oy', 'ok', 'om']\n",
    "for i,d in enumerate(data):\n",
    "    plt.plot(d[0], d[1], mark[result[i]])\n",
    "    \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
